package leetCode.thread.leetCode1115;

import java.util.concurrent.Semaphore;

/**
 * 信号量解法
 */
public class FooBar1 implements FooBar{

    private int n;

    Semaphore foo = new Semaphore(1);
    Semaphore bar = new Semaphore(0);


    @Override
    public void foo(Runnable printFoo) throws InterruptedException {
        for(int i=0;i<n;i++){
            foo.acquire();
            printFoo.run();
            bar.release();
        }
    }

    @Override
    public void bar(Runnable printBar) throws InterruptedException {
        for(int i=0;i<n;i++){
            bar.acquire();
            printBar.run();
            foo.release();
        }
    }
}
